﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TcpLabCommon;

namespace Common.GeneralClass
{
    public class AccelerationDB
    {
        public long Id;
        public string acqId;
        public string acqName;
        public int channelId;
        public Acceleration dataPaket=new Acceleration ();
        public DateTime insertTime;
        public int projectId;
        public int taskId;
    }




    public class OperateAccelerationDB
    {
        MyDb mydb = Mdl_Class.Mydb;
        MyDb mydb_del = Mdl_Class.DelMydb;
        MyDb mydb_insert = Mdl_Class.InsertMydb;
        MyDb mydb_other = Mdl_Class.Mydb_other;

        //插入振动加速度数据
        public int AddAcceleration(string tableName, AccelerationDB accelerdb)
        {
            int i = 0;
            using (mydb_insert)
            {
                try
                {
                    mydb_insert.Open();
            
                   

                    if (Mdl_Class.IsSaveFFTAmp)
                    {
                        byte[] b = Activator.CreateInstance<StructHelper>().Serialize(accelerdb.dataPaket);
                        i = mydb_insert.ExecuteNonQuery_acceleration(tableName, b, accelerdb);
                    }
                    else
                    {
                        Acceleration_NotSaveFFT acc = new Acceleration_NotSaveFFT();
                        acc.totaldB = accelerdb.dataPaket.totaldB;
                        acc._1_3octavedB = accelerdb.dataPaket._1_3octavedB;
                        acc.highdB = accelerdb.dataPaket.highdB;
                        acc.lowdB = accelerdb.dataPaket.lowdB;
                        acc.velocityRMS = accelerdb.dataPaket.velocityRMS;
                        acc.displacementRMS = accelerdb.dataPaket.displacementRMS;

                        accelerdb.acqId = accelerdb.acqId + "," + "NotSaveFFT";
                        byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acc);
                        i = mydb_insert.ExecuteNonQuery_acceleration(tableName, b, accelerdb);
                    }

                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddAcceleration()", ex.ToString());
                }
            }
            return i;
        }


        //批量插入振动加速度数据
        public int AddAccelerationBatch(string tableName, List<AccelerationDB> accelerdblist)
        {
            int addi = 0;
            using (mydb_insert)
            {
                try
                {
                    mydb_insert.Open();
                    //mydb_insert.BeginTransaction();
                    for (int i = 0; i < accelerdblist.Count;i++ )
                    {
                        //byte[] b = Activator.CreateInstance<StructHelper>().Serialize(accelerdblist[i].dataPaket);
                        //addi = mydb_insert.ExecuteNonQuery_acceleration(tableName, b, accelerdblist[i]);



                        if (Mdl_Class.IsSaveFFTAmp)
                        {
                            byte[] b = Activator.CreateInstance<StructHelper>().Serialize(accelerdblist[i].dataPaket);
                            i = mydb_insert.ExecuteNonQuery_acceleration(tableName, b, accelerdblist[i]);
                        }
                        else
                        {
                            Acceleration_NotSaveFFT acc = new Acceleration_NotSaveFFT();
                            acc.totaldB = accelerdblist[i].dataPaket.totaldB;
                            acc._1_3octavedB = accelerdblist[i].dataPaket._1_3octavedB;
                            acc.highdB = accelerdblist[i].dataPaket.highdB;
                            acc.lowdB = accelerdblist[i].dataPaket.lowdB;
                            acc.velocityRMS = accelerdblist[i].dataPaket.velocityRMS;
                            acc.displacementRMS = accelerdblist[i].dataPaket.displacementRMS;
                            accelerdblist[i].acqId = accelerdblist[i].acqId + "," + "0";
                            byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acc);
                            i = mydb_insert.ExecuteNonQuery_acceleration(tableName, b, accelerdblist[i]);
                        }

                        if (i > 0)
                        {
                            Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + 1;
                        }
                        else 
                        {
                            break;
                        }
                    }
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddAcceleration()", ex.ToString());
                }
            }
            return addi;
        }

        //根据条件查询ACQ数据
        public AccelerationDB GetAcceleration(int prjid, string acqid, int taskid,int channelid,string tableName,string time1)
        {
            
            AccelerationDB acq=null;
            using (MyDb mydb_select = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {

                    mydb_select.Open();
                    string sql = string.Format("select top 1 * from {0} where projectId={1} and acqId='{2}' and channelId='{3}'", tableName, prjid, acqid,channelid);

                    if (time1 != "")
                    {
                        sql = sql + string.Format(" and insertTime > '{0}'", time1);
                    }
             

                    sql = sql + " order by Id desc";
                    mydb_select.ExecuteReader(sql);
                    if (mydb_select.DataReader.Read())
                    {
                        acq = new AccelerationDB();
                        acq.Id = Convert.ToInt64(mydb_select.DataReader["Id"]);
                        acq.acqId = mydb_select.DataReader["acqId"].ToString();
                        acq.acqName = mydb_select.DataReader["acqName"].ToString();

                        byte[] by1 = mydb_select.DataReader["dataPaket"] as byte[];
                        if (acq.acqId.Split(',').Length == 2)
                        {
                            Acceleration_NotSaveFFT acc = Activator.CreateInstance<StructHelper>().DeSerialize<Acceleration_NotSaveFFT>(by1);


                            acq.dataPaket._1_3octavedB = acc._1_3octavedB;
                            acq.dataPaket.accelerationFFTAmpDB = new float[65536];
                            acq.dataPaket.highdB = acc.highdB;
                            acq.dataPaket.lowdB = acc.lowdB;
                            acq.dataPaket.totaldB = acc.totaldB;
                            acq.dataPaket.velocityRMS = acc.velocityRMS;
                            acq.dataPaket.displacementRMS = acc.displacementRMS;
                        }
                        else 
                        {
                            Acceleration dpk = Activator.CreateInstance<StructHelper>().DeSerialize<Acceleration>(by1);
                            acq.dataPaket = dpk;
                        }
                        acq.insertTime = Convert.ToDateTime(mydb_select.DataReader["insertTime"]);
                        acq.projectId = Convert.ToInt32(mydb_select.DataReader["projectId"]);
                        acq.channelId = channelid;
                        if (taskid != 0)
                        {
                            acq.taskId = Convert.ToInt32(mydb_select.DataReader["taskId"]);
                        }


                    
                    }
                    mydb_select.Close();
                }
                catch (Exception ex)
                {
                    acq = null;
                    Mdl_Class.WriteLog.dbErrLog("GetAllAcceleration()", ex.ToString());
                }
            }
            return acq;
        }



        //根据条件查询ACQ数据
        public List<AccDataInfo> GetAllAcceleration(int prjid, string tableName, string time1, string time2, List<int> channelidlist)
        {
            List<AccDataInfo> accelerationlist = new List<AccDataInfo>();

            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select * from {0} where 1=1 and ProjectId={1}", tableName, prjid);
                    if (channelidlist.Count > 0)
                    {
                        if (channelidlist.Count == 1)
                        {
                            sql = sql + string.Format(" and ChannelId={0}", channelidlist[0]+1);
                        }
                        else
                        {
                            sql = sql + string.Format(" and (ChannelId={0}", channelidlist[0]+1);
                            for (int i = 0; i < channelidlist.Count; i++)
                            {

                                sql = sql + string.Format(" or ChannelId={0}", channelidlist[i]+1);

                            }
                            sql = sql + ")";
                        }
                    }

 
                    if (time1 != "")
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", time1, time2);
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        AccDataInfo acq = new AccDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.TotaldB = Convert.ToSingle(mydb.DataReader["TotaldB"].ToString());
                        acq.HighdB = Convert.ToSingle(mydb.DataReader["HighdB"].ToString());
                        acq.LowdB = Convert.ToSingle(mydb.DataReader["LowdB"].ToString());
                        acq.VelocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"].ToString());
                        acq.AverageVol = Convert.ToSingle(mydb.DataReader["AverageVol"].ToString());
                        acq.DisplacementRMS = Convert.ToSingle(mydb.DataReader["DisplacementRMS"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.TwoMultiplierAmp = Convert.ToSingle(mydb.DataReader["TwoMultiplierAmp"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq._1_3octavedB = mydb.BytesToFloat(mydb.DataReader["_1_3octavedB"] as byte[]);
                        acq.FFTAmpDB = mydb.BytesToFloat(mydb.DataReader["FFTAmpDB"] as byte[]);
                        acq.DatapaketPath = mydb.DataReader["DatapaketPath"].ToString();
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());
                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        accelerationlist.Add(acq);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllAcceleration()", ex.ToString());
                }
            }
            return accelerationlist;
        }



        public List<AccDataInfo> GetAllAccelerationByTime(int prjid, string tableName, string time1, string time2, List<int> channelidlist)
        {
            List<AccDataInfo> accelerationlist = new List<AccDataInfo>();

            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select Id,ChannelId,TotaldB,HighdB,LowdB,VelocityRMS,AverageVol,DisplacementRMS,DatapaketPath,InsertTime,ProjectId,Rpm1,Rpm2,Rpm3,Rpm4,Rpm5,Rpm6 from {0} where 1=1 and ProjectId={1}", tableName, prjid);
                    if (channelidlist.Count > 0)
                    {
                        if (channelidlist.Count == 1)
                        {
                            sql = sql + string.Format(" and ChannelId={0}", channelidlist[0]+1);
                        }
                        else
                        {
                            sql = sql + string.Format(" and (ChannelId={0}", channelidlist[0]+1);
                            for (int i = 0; i < channelidlist.Count; i++)
                            {

                                sql = sql + string.Format(" or ChannelId={0}", channelidlist[i]+1);

                            }
                            sql = sql + ")";
                        }
                    }


                    if (time1 != "")
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", time1, time2);
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        AccDataInfo acq = new AccDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.TotaldB = Convert.ToSingle(mydb.DataReader["TotaldB"].ToString());
                        acq.HighdB = Convert.ToSingle(mydb.DataReader["HighdB"].ToString());
                        acq.LowdB = Convert.ToSingle(mydb.DataReader["LowdB"].ToString());
                        acq.VelocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"].ToString());
                        acq.AverageVol = Convert.ToSingle(mydb.DataReader["AverageVol"].ToString());
                        acq.DisplacementRMS = Convert.ToSingle(mydb.DataReader["DisplacementRMS"].ToString());
                        acq.DatapaketPath = mydb.DataReader["DatapaketPath"].ToString();
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());
                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        accelerationlist.Add(acq);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllAcceleration()", ex.ToString());
                }
            }
            return accelerationlist;
        }

        //根据条件查询Acceleration数据分页
        public List<AccDataInfo> GetAllAccelerationByPage(int prjid,string tableName, string dt1, string dt2, long pagevalue, long pagesize,int channelid)
        {
            List<AccDataInfo> AccelerationDBlist = new List<AccDataInfo>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "";
                    if (pagevalue == 0)
                    {
                        pagevalue = 1;
                    }
                    if (dt1 != null)
                    {
                        if (channelid >= 0)
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where InsertTime  between '{4}' and '{5}' and ProjectId={6} and ChannelId={7}) and ProjectId={8}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, dt1, dt2, prjid, channelid+1, prjid);
                        }
                        else
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where InsertTime  between '{4}' and '{5}' and ProjectId={6}) and ProjectId={7}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, dt1, dt2, prjid, prjid);
                        }
                    }
                    else
                    {
                        if (channelid >= 0)
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where ProjectId={4} and ChannelId={5}) and ProjectId={6}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, prjid, channelid+1, prjid);
                        }
                        else
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where ProjectId={4}) and ProjectId={5}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, prjid, prjid);
                        }
                    }
                    if (channelid >= 0)
                    {
                        sql = sql + " and  ChannelId=" + (channelid+1).ToString();
                    }
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1, dt2);
                    }
                    sql = sql + " order by Id";

                    mydb.ExecuteReader(sql);

                    while (mydb.DataReader.Read())
                    {
                        AccDataInfo acq = new AccDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.TotaldB = Convert.ToSingle(mydb.DataReader["TotaldB"].ToString());
                        acq.HighdB = Convert.ToSingle(mydb.DataReader["HighdB"].ToString());
                        acq.LowdB = Convert.ToSingle(mydb.DataReader["LowdB"].ToString());
                        acq.VelocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"].ToString());
                        acq.AverageVol = Convert.ToSingle(mydb.DataReader["AverageVol"].ToString());
                        acq.DisplacementRMS = Convert.ToSingle(mydb.DataReader["DisplacementRMS"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.TwoMultiplierAmp = Convert.ToSingle(mydb.DataReader["TwoMultiplierAmp"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq._1_3octavedB = mydb.BytesToFloat(mydb.DataReader["_1_3octavedB"] as byte[]);
                        acq.FFTAmpDB = mydb.BytesToFloat(mydb.DataReader["FFTAmpDB"] as byte[]);
                        acq.DatapaketPath = mydb.DataReader["DatapaketPath"].ToString();
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());
                        int speedid = Mdl_Class.GetSpeedId(channelid);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;
                        AccelerationDBlist.Add(acq);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllAccelerationByPage()", ex.ToString());
                }
            }
            return AccelerationDBlist;
        }



        /// <summary>
        /// 根据测定查询
        /// </summary>
        /// <param name="prjid"></param>
        /// <param name="tableName"></param>
        /// <param name="date"></param>
        /// <param name="channel1"></param>
        /// <param name="channel2"></param>
        /// <returns></returns>
        public Dictionary<int, AccDataInfo> GetAllAccelerationByTestPoint(int prjid, string tableName, DateTime date, int channel1, int channel2)
        {
            Dictionary<int, AccDataInfo> acqlist = new Dictionary<int, AccDataInfo>();
            AccDataInfo channe11_value = new AccDataInfo();
            AccDataInfo channe12_value = new AccDataInfo();
            acqlist.Add(channel1, channe11_value);
            acqlist.Add(channel2, channe12_value);

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select * from {0} where ProjectId={1} and InsertTime='{1}' and (ChannelId={2} or ChannelId={3})",
                               tableName, date, channel1, channel2);

                    sql = sql + " order by Id";

                    mydb.ExecuteReader(sql);

                    while (mydb.DataReader.Read())
                    {
                        AccDataInfo acq = new AccDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.TotaldB = Convert.ToSingle(mydb.DataReader["TotaldB"].ToString());
                        acq.HighdB = Convert.ToSingle(mydb.DataReader["HighdB"].ToString());
                        acq.LowdB = Convert.ToSingle(mydb.DataReader["LowdB"].ToString());
                        acq.VelocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"].ToString());
                        acq.AverageVol = Convert.ToSingle(mydb.DataReader["AverageVol"].ToString());
                        acq.DisplacementRMS = Convert.ToSingle(mydb.DataReader["DisplacementRMS"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.TwoMultiplierAmp = Convert.ToSingle(mydb.DataReader["TwoMultiplierAmp"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq._1_3octavedB = mydb.BytesToFloat(mydb.DataReader["_1_3octavedB"] as byte[]);
                        acq.FFTAmpDB = mydb.BytesToFloat(mydb.DataReader["FFTAmpDB"] as byte[]);
                        acq.DatapaketPath = mydb.DataReader["DatapaketPath"].ToString();
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());
                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        if (acqlist.ContainsKey(acq.ChannelId))
                        {
                            acqlist[acq.ChannelId] = acq;
                        }
                   
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllAccelerationByPage()", ex.ToString());
                }
            }
            return acqlist;
        }


        //根据条件查询Acceleration数据获取总数量
        public long GetAllAccelerationPageSum(int prjid, string tableName,  string dt1, string dt2, int channelid)
        {
            long pagesum = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select count(Id) as pagecount from {0} where 1=1 and ProjectId={1}", tableName, prjid);

                    if (channelid >= 0)
                    {
                        sql = sql + string.Format(" and ChannelId={0}", channelid);
                    }
          
                    if (dt1 != null && dt1!="")
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", dt1, dt2);
                    }
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        pagesum = Convert.ToInt64(mydb.DataReader["pagecount"]); ;
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" GetAllAccelerationPageSum()", ex.ToString());
                }
            }
            return pagesum;
        }



        /// <summary>
        /// 通过ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelACC(List<long> idlsit, string tablename)
        {
            int delcount = 0;

            using (MyDb mydb_del = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb_del.Open();
                    mydb_del.BeginTransaction();
                    string sql = string.Format("delete from {0}", tablename);
                    for (int i = 0; i < idlsit.Count; i++)
                    {
                        if (i > 1000)
                        {
                            delcount = mydb_del.ExecuteNonQuery(sql);
                            sql = string.Format("delete from {0} where Id={1}", tablename, idlsit[i]);
                        }
                        else
                        {
                            if (i == 0)
                            {
                                sql = sql + string.Format(" where Id={0}", idlsit[i]);
                            }
                            else
                            {
                                sql = sql + string.Format(" or Id={0}", idlsit[i]);
                            }
                        }

                    }
                    delcount = mydb_del.ExecuteNonQuery(sql);
                    if (delcount > 0)
                    {
                        mydb_del.Commit();
                    }
                    else
                    {
                        mydb_del.Rollback();
                    }

                    mydb_del.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DelACC(List<int> idlsit,string tablename)", ex.ToString());
                }
            }
            return delcount;
        }

        //根据条件查询ACQ数据
        public Dictionary<int,ACQSpeed> GetAllACQSpeed(List<int> channelidlist,int prjid, string tableName, string time1, string time2)
        {
            Dictionary<int, ACQSpeed> channelToSpeed = new Dictionary<int, ACQSpeed>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(time1).Ticks);
                    TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(time2).Ticks);
                    TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                    if (ts3.TotalHours > 12)
                    {
                        tableName = "View_Acc";
                    }
                    else
                    {
                        tableName = "View_Acc_Day";
                    }
                    string sql = string.Format("select * from {0} where 1=1 and ProjectId={1}", tableName, prjid);
           
                        for (int i = 0; i < channelidlist.Count; i++)
                        {
                            if (i == 0 && channelidlist.Count > 1)
                            {
                                sql = sql + string.Format(" and (ChannelId={0}", channelidlist[i]+1);
                            }
                            else if (i == 0 && channelidlist.Count == 1)
                            {
                                sql = sql + string.Format(" and ChannelId={0}", channelidlist[i]+1);
                            }
                            else if (i > 0 && i == channelidlist.Count - 1)
                            {
                                sql = sql + string.Format(" or ChannelId={0})", channelidlist[i]+1);
                            }
                            else if (i > 0 && channelidlist.Count > 1)
                            {
                                sql = sql + string.Format(" or ChannelId={0}", channelidlist[i]+1);
                            }
                        }
                    
                    if (time1 != "")
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", time1, time2);
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    ACQSpeed acqspeed = null;
                    while (mydb.DataReader.Read())
                    {
                        int channelid = Convert.ToInt32(mydb.DataReader["ChannelId"]);
                        if (!channelToSpeed.ContainsKey(channelid))
                        {

                            ACQSpeed acqspeed1 = new ACQSpeed();
                            acqspeed1.channelid = channelid;
                            channelToSpeed.Add(channelid, acqspeed1);
                        }
                        acqspeed = channelToSpeed[channelid];
                        int speedid = Mdl_Class.GetSpeedId(channelid);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }

                        float totaldB = Convert.ToSingle(mydb.DataReader["TotaldB"]);
                        float highdB = Convert.ToSingle(mydb.DataReader["HighdB"]);
                        float lowdB = Convert.ToSingle(mydb.DataReader["LowdB"]);
                        float velocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"]);
                        DateTime insertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"]);
                        float displacementRMS = Convert.ToSingle(mydb.DataReader["DisplacementRMS"]);
                        acqspeed.rpm.Add(rpm_value);
                        acqspeed.totaldB.Add(totaldB);
                        acqspeed.highdB.Add(highdB);
                        acqspeed.lowdB.Add(lowdB);
                        acqspeed.velocityRMS.Add(velocityRMS);
                        acqspeed.insertTime.Add(insertTime);
                        acqspeed.displacementRMS.Add(displacementRMS);
                   
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQSpeed()", ex.ToString());
                }
            }
            return channelToSpeed;
        }


        public void GetAllACQSpeedByField(ref Dictionary<int, List<ACQSpeed_Field>> channelToACQ, int channelId, int prjid, string tableName, DateTime dt1, DateTime dt2, string fieldName)
        {
            //Mdl_Class.speedtotimelist.Clear();
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();

                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                    TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                    if (ts3.TotalHours > 12)
                    {
                        tableName = "View_Acc_Day";
                    }
                    else
                    {
                        tableName = "View_Acc";
                    }
                    int speedid = Mdl_Class.GetSpeedId(channelId);
                    string rpm_fieldName = "Rpm1";
                    switch (speedid)
                    {
                        case 1:
                            rpm_fieldName = "Rpm1";
                            break;
                        case 2:
                            rpm_fieldName = "Rpm2";
                            break;
                        case 3:
                            rpm_fieldName = "Rpm3";
                            break;
                        case 4:
                            rpm_fieldName = "Rpm4";
                            break;
                        case 5:
                            rpm_fieldName = "Rpm5";
                            break;
                        case 6:
                            rpm_fieldName = "Rpm6";
                            break;
                        default:
                            break;
                    }

                    string sql = string.Format("select ChannelId,{0},{1},InsertTime from {2} where  ProjectId={3}", fieldName, rpm_fieldName, tableName, prjid);
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    if (channelId >= 0)
                    {
                        sql = sql + string.Format(" and ChannelId={0}", channelId+1);
                    }
                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    ACQData acq = null;
                    SpeedToTime sp = null;
           
                    List<ACQSpeed_Field> acqdatalist = new List<ACQSpeed_Field>();
                    List<string> datetimelist = new List<string>();
                    while (mydb.DataReader.Read())
                    {
                        if (!datetimelist.Contains(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss")))
                        {

                            datetimelist.Add(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss"));
                       
                                ACQSpeed_Field acq_field = new ACQSpeed_Field();
                                acq_field.channelid = Convert.ToInt32(mydb.DataReader["ChannelId"]);
                                acq_field.Rpms = Convert.ToSingle(mydb.DataReader[rpm_fieldName]);
                                acq_field.FieldValues = Convert.ToSingle(mydb.DataReader[fieldName]);
                                acq_field.date1 = Convert.ToDateTime(mydb.DataReader["InsertTime"]);


                                acqdatalist.Add(acq_field);
                       
                            
                        }
                    }
                    if (channelToACQ.ContainsKey(channelId))
                    {
                        channelToACQ[channelId] = acqdatalist;

                    }
                    else
                    {
                        channelToACQ.Add(channelId, acqdatalist);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQSpeedByField()", ex.ToString());
                }
            }
            //return channelToACQ;
        }
    }
}
